/**
 * GTBarButtonWithActiveIcon.h
 *
 * The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License 
 * at http://www.mozilla.org/MPL/
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
 * either express or implied. See the License for the specific language governing rights and limitations 
 * under the License.
 *
 * The Original Code is gcast-toolbox4iphone.
 *
 * The Initial Developers of the Original Code is Giannandrea Castaldi.
 */

#import "GTBarBorderButtonWithActiveColor.h"
#import "UIImage+GTExtension.h"

@interface GTBarButtonWithUIBorderButton ()
@property(nonatomic,retain,readonly) UIButton *button;
@end

@interface GTBarBorderButtonWithActiveColor ()
@property(nonatomic,retain,readwrite) UIImage *originalImage;
@property(nonatomic,retain) UIColor *colorForActiveState;  
- (UIImage*)imageToShow;
@end

@implementation GTBarBorderButtonWithActiveColor

@synthesize active, originalImage, colorForActiveState;

- (id)initWithTitle:(NSString*)aTitle font:(UIFont*)aFont icon:(UIImage*)anIcon colorForActiveState:(UIColor*)aColorForActiveState {
  if ((self = [super initWithTitle:aTitle font:aFont icon:anIcon])) {
    self.colorForActiveState = aColorForActiveState;
    self.originalImage = anIcon;
  }
  return self;
}

- (id)initWithTitle:(NSString*)aTitle font:(UIFont*)aFont icon:(UIImage*)anIcon buttonWidth:(CGFloat)buttonWidth colorForActiveState:(UIColor*)aColorForActiveState {
  if ((self = [super initWithTitle:aTitle font:aFont icon:anIcon buttonWidth:buttonWidth])) {
    self.colorForActiveState = aColorForActiveState;
    self.originalImage = anIcon;
  }
  return self;
}

- (id)initWithTitle:(NSString*)aTitle font:(UIFont*)aFont colorForActiveState:(UIColor*)aColorForActiveState {
  if ((self = [super initWithTitle:aTitle font:aFont])) {
    self.colorForActiveState = aColorForActiveState;
  }
  return self;
}

- (id)initWithTitle:(NSString*)aTitle font:(UIFont*)aFont buttonWidth:(CGFloat)buttonWidth colorForActiveState:(UIColor*)aColorForActiveState {
  if ((self = [super initWithTitle:aTitle font:aFont buttonWidth:buttonWidth])) {
    self.colorForActiveState = aColorForActiveState;
  }
  return self;
}

- (void)dealloc {
  [originalImage release];
  [colorForActiveState release];
  [super dealloc];
}

- (void)activate {
  active = TRUE;
  [self.button setTitleColor:self.colorForActiveState forState:UIControlStateNormal];
  [self.button setImage:[self imageToShow] forState:UIControlStateNormal];  
}

- (void)deactivate {
  active = FALSE;
  [self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  [self.button setImage:[self imageToShow] forState:UIControlStateNormal];  
}

- (UIImage*)imageToShow {
  UIImage* result = nil;
  if (self.active) {
    result = [self.originalImage paintWithColor:self.colorForActiveState];
  }
  else {
//    result = [[[UIImage alloc] initWithCGImage:[self.originalImage CGImage]] autorelease];
    result = [self.originalImage paintWithColor:[UIColor whiteColor]];
  }
  return result;
}

@end
